<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :inline="true" :model="searchObj" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.dishName" placeholder="菜品名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.category" placeholder="菜品类型"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.subCategory" placeholder="菜系"></el-input>
      </el-form-item>
      <el-select v-model="searchObj.sort" placeholder="排序规则">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <!--点击查询按钮后提交到Onsubmit方法处理-->
      <el-button type="primary" @click="getByPage">查询</el-button>
      <el-button type="success" @click="reset">重置</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="菜品名称" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="img" label="菜品图片" width="150" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.dishPicture" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="菜品一级分类" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishClassification }}</span>
        </template>
      </el-table-column>
      <el-table-column label="菜品二级分类" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishSubclassification }}</span>
        </template>
      </el-table-column>
      <el-table-column label="菜品价格" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所需时间" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存" width="130" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="addToCart(scope.row.id)">加入购物车</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页工具条 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>
<script>
import dish from "@/api/dish";
import shoppingCart from "@/api/shoppingcart";

export default {
  data() {
    return {
      multipleSelection: [], //存储被选中的每一个对象
      selectIds: "", //存储被选中的每一个对象的id
      listLoading: true, //是否显示loading信息
      list: [], //菜品数据列表
      page: 1, //页码
      limit: 5, //每页记录数
      total: 0, //总记录数
      currentPage: 1,
      searchObj: {},
      options: [
        {
          value: "1",
          label: "时间从长到短"
        },
        {
          value: "2",
          label: "时间从短到长"
        },
        {
          value: "3",
          label: "价格从高到低"
        },
        {
          value: "4",
          label: "价格从低到高"
        }
      ],
      dish: {},
      dialogDishFormVisible: false,
      saveBtnDisabled: false //保存按钮是否禁用
    };
  },
  mounted() {
    this.getByPage();
  },
  methods: {
    addToCart(row) {
      shoppingCart.addToCart(row).then(response => {
        if (response.success) {
          this.$message({
            type: "success",
            message: "加入购物车成功"
          });
        } else {
          this.$message({
            type: "warning",
            message: response.message
          });
        }
      });
    },
    reset() {
      this.searchObj = {};
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getByPage();
    },
    handleCurrentChange(val) {
      (this.page = val), this.getByPage();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //带条件的分页
    getByPage() {
      dish
        .selectPageByConditionApi(this.page, this.limit, this.searchObj)
        .then(response => {
          this.list = response.data.pageInfo.records;
          this.total = response.data.pageInfo.total;
          this.listLoading = false;
        });
    }
  }
};
</script>
